package com.xyj.zjbz.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.xyj.zjbz.constants.Constants;
import com.xyj.zjbz.po.Role;
import com.xyj.zjbz.po.User;
import com.xyj.zjbz.po.ZJDataSource;
import com.xyj.zjbz.user.service.UserService;
import com.xyj.zjbz.util.DataSourceSwitcher;
import com.xyj.zjbz.util.MySqlUtil;
import com.xyj.zjbz.vo.MysqlUrl;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
// 保存session
public class UserCTL {
	@Autowired
	private UserService userService;
	@Autowired
	Constants c;

	@RequestMapping(value = "/valid", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> validUser(HttpServletRequest request,
			ModelMap modelMap) {
		DataSourceSwitcher.setDataSource("sys");// 切换数据源
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		User user = new User();
		user.setLoginName(userName);
		user.setPwd(pwd);
		Map<String, Object> map = userService.validUser(user);
		int status = Integer.valueOf((String) map.get("status"));
		if (status == 1) {
			User u = (User) map.get("user");
			u.setPwd(null);
			modelMap.addAttribute("user", u);// 存入session,注意这个值不可以为null
		}
		return map;
	}

	/**
	 * 获取对应的db
	 * 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public ModelAndView getInfo(ModelMap modelMap,
			@ModelAttribute(value = "user") User user) {
		ModelAndView mv = new ModelAndView();

		//获取角色信息
		List<Role> roles= userService.getRolesByUser(user);
		mv.addObject("roles", roles);
		mv.setViewName("info");
		return mv;
	}
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(ModelMap modelMap, SessionStatus status) {
		modelMap.remove("user");
		status.setComplete();//销毁session
		return "login";
	}

	@RequestMapping(value = "/toAddUser", method = RequestMethod.GET)
	public String toAddUser(ModelMap modelMap, SessionStatus status) {
		return "/user/addUser";
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public String addUser(ModelMap modelMap, SessionStatus status) {
		System.out.println("addUser......");
		return "";
	}
	
	/**
	 * 添加datasource
	 * @param modelMap
	 * @param ds
	 * @return	 */
	@RequestMapping(value = "/addDs", method = RequestMethod.POST)
	public String addDataSource(ModelMap modelMap ,ZJDataSource ds) {
		System.out.println(ds.getBzDB());
		//组装url
		String url="jdbc:mysql://" +c.getMysqlIp()+":"+c.getMysqlPort()+"/"+ds.getBzDB();
		ds.setDbUrl(url);
		ds.setStatus("1");
		DataSourceSwitcher.setDataSource("sys");
		Map<String, Object> map=  userService.addDataSource(ds);
		MysqlUrl mysqlAdmin = new MysqlUrl(c.getMysqlAdminUser(),
				c.getMysqlAdminPwd(), c.getMysqlIp(), c.getMysqlPort(),
				ds.getBzDB());
		MySqlUtil.createDataSource(ds, mysqlAdmin);
		modelMap.addAllAttributes(map);
		return "success";
	}
	
	@RequestMapping("toAddDs")
	public String toAddDs(){
		return "addDataSource";
	}
	
}
